package com.sh4.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.Article;
import com.sh4.service.ArticleService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value="article")
public class ArticleController {
	
	@Autowired
	ArticleService articleService;
	
	@RequestMapping(value = "/" ,  method = RequestMethod.POST)
	public Map<String, Object> addArticle(@RequestBody Article article) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			articleService.save(article);
			result.put("data", article);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value = "/list" ,  method = RequestMethod.POST)
	public Map<String, Object> articleList(@RequestParam int pageSize , @RequestParam int pageNo,@RequestBody JSONObject reqBody) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功";
		try {
			
			result.put("dataList", articleService.list(pageSize, pageNo, reqBody));
			result.put("total", articleService.countAtricle(pageSize, pageNo, reqBody));
			
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value = "/" ,  method = RequestMethod.DELETE)
	public Map<String, Object> deleteArticle(@RequestBody Article article) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "删除成功";
		try {
			
			articleService.deleteArticle(article);
			result.put("data", article);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value = "/detail" ,  method = RequestMethod.POST)
	public Map<String, Object> articleDetail(@RequestBody JSONObject reqBody) {
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "请求成功";
		try {
			String articleId = reqBody.getString("id");
			String type = reqBody.getString("type");
			
			Article a = articleService.getArticleById(articleId,type);
			if(a == null) {
				status = 300;
				msg = "此详情不存在";
			}else {
				result.put("data", a);
				result.put("preData", articleService.preArticle(a.getCreateDate() , a.getType()));
				result.put("suffixData", articleService.suffixArticle(a.getCreateDate() , a.getType()));
			}
			
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status);
		result.put("msg", msg);
		
		return result;
	}

}
